﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Demo.Site;
using Blank.Http.Enumrator;

namespace Demo
{
    public partial class MiForm : Form
    {
        private BindingList<MiSite> _miSites = new BindingList<MiSite>();

        public BindingList<MiSite> MiSites
        {
            get { return _miSites; }
        }

        public MiForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiForm_Load(object sender, EventArgs e)
        {
            this.dgvSearchResult.AutoGenerateColumns = false;
            this.dgvSearchResult.DataBindings.Add("DataSource", this, "MiSites");
            MiSite site1 = new MiSite() { UserName = "262879651@qq.com", Password = "cf146725896" };
            site1.LoginCompleted += new EventHandler(site1_LoginCompleted);
            MiSites.Add(site1);
        }

        private void site1_LoginCompleted(object sender, EventArgs e)
        {
            MiSite site = (MiSite)sender;
            if (site.LoginResult != null)
            {
                if (site.LoginResult.desc.Equals("成功"))
                    site.LoginState = LoginState.已登录;
                else
                    site.LoginState = LoginState.登陆出错;

            }
            else
                site.LoginState = LoginState.登陆出错;

        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (MiSites[0].LoginState != LoginState.登出)
                MiSites[0].LoginState = LoginState.已注销登陆;
            else
                MiSites[0].LoginState = LoginState.已登录;
            this.dgvSearchResult.Refresh();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSearchResult_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                MiSite miSite = this.MiSites[e.RowIndex];
                miSite.OnShowMessageCompleted += new EventHandler<Blank.Http.EventArg.ShowMessageEventArgs>(miSite_OnShowMessageCompleted);
                miSite.Login();
            }
        }

        void miSite_OnShowMessageCompleted(object sender, Blank.Http.EventArg.ShowMessageEventArgs e)
        {
            MessageBox.Show(e.Message);
        }
    }
}
